﻿/**************************************************
 *  Author :    youping
 *  CreateDate: 2020-10-11
 *  Title:      系统相关公共方法
**************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo.FolderOp
{
    public class CommonMethod
    {
        public static string IsPathUrlRight(string path)
        {
            string result = string.Empty;
            if (string.IsNullOrWhiteSpace(path))
            {
                result = "请选择文件路径地址";
            }
            if (!Directory.Exists(path))
            {
                result = "请输入正确的文件路径地址";
            }
            return result;
        }

        #region public DataTable GetDateDt(string startDate, string endDate, string format = "yyyy-MM-dd") 获取指定时间隔的时间数据集合  youping 2021-03-12 17:09
        /// <summary>
        /// 获取指定时间隔的时间数据集合
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static DataTable GetDateDt(string startDate, string endDate, string format = "yyyy-MM-dd")
        {
            if (string.IsNullOrWhiteSpace(startDate) || string.IsNullOrWhiteSpace(endDate))
            {
                return null;
            }
            DataTable myDateDt = new DataTable();
            myDateDt.Columns.Add("Date", typeof(string));

            DateTime dStartDate;
            DateTime dEndDate;
            DataRow myNewDr = null;
            try
            {
                dStartDate = Convert.ToDateTime(startDate);
                dEndDate = Convert.ToDateTime(endDate);
                if (dEndDate < dStartDate)
                {
                    return null;
                }

                do
                {
                    myNewDr = myDateDt.NewRow();
                    myNewDr["Date"] = Convert.ToDateTime(dStartDate).ToString(format);
                    myDateDt.Rows.Add(myNewDr);
                    dStartDate = dStartDate.AddDays(1);
                }
                while (dStartDate <= dEndDate);
                return myDateDt;
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region public static string DateFormat(string paramDate, string formart = "yyyy-MM-dd") 时间格式化
        /// <summary>
        /// 时间格式化
        /// </summary>
        /// <param name="paramDate">时间字符串</param>
        /// <param name="formart">格式</param>
        /// <returns></returns>
        public static string DateFormat(string paramDate, string formart = "yyyy-MM-dd")
        {
            if (string.IsNullOrWhiteSpace(paramDate))
            {
                return string.Empty;
            }
            else
            {
                paramDate = paramDate.Replace(".", "-").Replace("/", "-").Replace("年", "-").Replace("月", "-").Replace("日", "");
            }
            try
            {
                DateTime resultDt = new DateTime();
                if (!DateTime.TryParse(paramDate, out resultDt))
                {
                    return string.Empty;
                }
                if (resultDt.Equals(DateTime.Parse("1900-01-01 00:00:00")))
                {
                    return string.Empty;
                }
                if (resultDt.Equals(DateTime.Parse("1900-01-01")))
                {
                    return string.Empty;
                }
                if (!string.IsNullOrEmpty(formart)) { return Convert.ToDateTime(paramDate).ToString(formart); }
                else { return paramDate; }
            }
            catch// (Exception myEx)
            {
                //myEx.Out("CommonMethod-GetDateString:" + myEx.Message);
                return string.Empty;
            }
        }
        #endregion

        #region public static string ImportTip(DateTime startDateTime, DateTime endDateTime)  计算时间
        /// <summary>
        /// 计算时间
        /// </summary>
        /// <param name="startDateTime"></param>
        /// <param name="endDateTime"></param>
        /// <returns></returns>
        public static string ImportTip(DateTime startDateTime, DateTime endDateTime)
        {
            string result = string.Empty;
            try
            {
                TimeSpan myTs = endDateTime - startDateTime;
                result = (myTs.Hours > 0 ? (myTs.Hours.ToString() + "小时") : "") + (myTs.Minutes > 0 ? myTs.Minutes.ToString() + "分" : "") + (myTs.Seconds > 0 ? myTs.Seconds.ToString() + "秒" : "");

                if (string.IsNullOrWhiteSpace(result))
                {
                    result = "秒操作";
                }
                else
                {
                    result = "共计操作时间：" + result;
                }
            }
            catch
            {
            }
            return result;
        }
        #endregion

        #region 添加成员
        public static void ListAdd(List<string> myList, string member)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(member) && myList.IndexOf(member) < 0)
                {
                    myList.Add(member);
                }
            }
            catch (Exception myEx)
            {
                throw myEx;
            }
        }
        #endregion

    }
}
